package com.fwpj.service;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.common.bo.Node;
import com.common.util.ObjectUtils;
import com.common.util.ObjectXmlUtils;
import com.common.util.impl.ObjectXmlUtilsAttributeImpl;
import com.core.service.AppBaseService;
import com.fwpj.entity.*;
import com.fwpj.service.SVC_FWPJLocal;
import com.fwpj.service.SVC_FWPJRemote;
import com.fwpj.rule.*;

/**
 * 商户管理
 */
@Stateless
public class SVC_FWPJ extends AppBaseService implements SVC_FWPJRemote,
		SVC_FWPJLocal {
	@Resource(name = "JmsConnectionFactory")
	private ConnectionFactory connectionFactory;
	
	@Override//添加行业分类
	public Map ADD_HYFL(Map param) {
		TB_FWPJ_HYFL bo= ObjectUtils.mapToJavaBean(param,TB_FWPJ_HYFL.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_FWPJ_HYFL.STATUS_ACTIVITY);
		getJpaDao().save(bo);

		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_HYFL(Map param) {

		TB_FWPJ_HYFL dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_HYFL.class);
		TB_FWPJ_HYFL po = getJpaDao().find(TB_FWPJ_HYFL.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);

		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_HYFL(Map param) {
		TB_FWPJ_HYFL dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_HYFL.class);
		TB_FWPJ_HYFL po = getJpaDao().find(TB_FWPJ_HYFL.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_FWPJ_HYFL.STATUS_DEAD);
		getJpaDao().update(po);
	}
	
	@Override//添加评级分类
	public Map ADD_PJFL(Map param) {
		TB_FWPJ_PJFL bo= ObjectUtils.mapToJavaBean(param,TB_FWPJ_PJFL.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_FWPJ_PJFL.STATUS_ACTIVITY);
		getJpaDao().save(bo);

		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_PJFL(Map param) {

		TB_FWPJ_PJFL dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_PJFL.class);
		TB_FWPJ_PJFL po = getJpaDao().find(TB_FWPJ_PJFL.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);

		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_PJFL(Map param) {
		TB_FWPJ_PJFL dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_PJFL.class);
		TB_FWPJ_PJFL po = getJpaDao().find(TB_FWPJ_PJFL.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_FWPJ_PJFL.STATUS_DEAD);
		getJpaDao().update(po);
	}
	
	
	@Override
	public Map ADD_LXDY(Map param) {
		TB_FWPJ_LXDY bo = ObjectUtils.mapToJavaBean(param, TB_FWPJ_LXDY.class);
		if (bo.getID() == null) {
			bo.setID(bo.uuid());
		}
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setLASTSTATE(0);
		bo.setSTATUS(TB_FWPJ_LXDY.STATUS_ACTIVITY);
		getJpaDao().save(bo);

		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_LXDY(Map param) {

		TB_FWPJ_LXDY dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_LXDY.class);
		TB_FWPJ_LXDY po = getJpaDao().find(TB_FWPJ_LXDY.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));

		po.setLASTSTATE(dto.getLASTSTATE());
		getJpaDao().update(po);

		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_LXDY(Map param) {
		TB_FWPJ_LXDY dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_LXDY.class);
		TB_FWPJ_LXDY po = getJpaDao().find(TB_FWPJ_LXDY.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_FWPJ_LXDY.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map GetParent(Map param) {

		TB_FWPJ_LXDY dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_LXDY.class);

		TB_FWPJ_LXDY po = getJpaDao().find(TB_FWPJ_LXDY.class, dto.getID());
		if (po.getPARENTID() != null) {
			TB_FWPJ_LXDY bo = getJpaDao().find(TB_FWPJ_LXDY.class,
					po.getPARENTID());
			return ObjectUtils.javaBeanToMap(bo);
		} else {
			return ObjectUtils.javaBeanToMap(po);
		}
	}

	@Override
	public Map ADD_LXDY_KZSXZ(Map param) {

		TB_FWPJ_LXDY_KZSXZ bo = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_LXDY_KZSXZ.class);
		bo.setID(bo.uuid());
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setCREATE_TIME(getOpTime(param));
		bo.setHYFLID(bo.getHYFLID());
		bo.setLXDYID(bo.getLXDYID());
		getJpaDao().save(bo);

		TB_FWPJ_LXDY po = getJpaDao().find(TB_FWPJ_LXDY.class, bo.getLXDYID());
		po.setLASTSTATE(1);
		getJpaDao().update(po);

		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map ADD_LXDY_KZSXZ_SQ(Map param) {
		List SQ_SXZFWLIST = ObjectUtils.getMapValue(param,
				"SQ_SXZFWLIST", List.class);
		if (SQ_SXZFWLIST != null) {
			for (Object item : SQ_SXZFWLIST) {
				TB_FWPJ_SQDY_KZSXZ kzsx = ObjectUtils.mapToJavaBean(
						(Map) item, TB_FWPJ_SQDY_KZSXZ.class);
				kzsx.setID(kzsx.uuid());
				kzsx.setCREATE_TIME(getOpTime(param));
				kzsx.setSTATUS(TB_FWPJ_LXDY.STATUS_ACTIVITY);
				getJpaDao().save(kzsx);
				((Map) item).put("ID", kzsx.getID());
			}
			return param;
			}
		else{
		TB_FWPJ_SQDY_KZSXZ bo = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_SQDY_KZSXZ.class);
		bo.setID(bo.uuid());
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		bo.setCREATE_TIME(getOpTime(param));
		bo.setHYFLID(bo.getHYFLID());
		bo.setLXDYID(bo.getLXDYID());
		getJpaDao().save(bo);
		return ObjectUtils.javaBeanToMap(bo);
		}

		
	}
	@Override
	public Map UPD_LXDY_KZSXZ(Map param) {
		TB_FWPJ_LXDY_KZSXZ dto = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_LXDY_KZSXZ.class);
		TB_FWPJ_LXDY_KZSXZ po = getJpaDao().find(TB_FWPJ_LXDY_KZSXZ.class,
				dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME", "HYFLID",
				"LXDYID");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public Map UPD_LXDY_KZSXZ_SQ(Map param) {
		TB_FWPJ_SQDY_KZSXZ dto = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_SQDY_KZSXZ.class);
		TB_FWPJ_SQDY_KZSXZ po = getJpaDao().find(TB_FWPJ_SQDY_KZSXZ.class,
				dto.getID());
		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME", "HYFLID",
				"LXDYID");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_LXDY_KZSXZ(Map param) {
		TB_FWPJ_LXDY_KZSXZ dto = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_LXDY_KZSXZ.class);
		TB_FWPJ_LXDY_KZSXZ po = getJpaDao().find(TB_FWPJ_LXDY_KZSXZ.class,
				dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_FWPJ_LXDY.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public void DEL_LXDY_KZSXZ_SQ(Map param) {
		TB_FWPJ_SQDY_KZSXZ dto = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_SQDY_KZSXZ.class);
		TB_FWPJ_SQDY_KZSXZ po = getJpaDao().find(TB_FWPJ_SQDY_KZSXZ.class,
				dto.getID());
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(-1);
		getJpaDao().update(po);
	}

	@Override
	public Map ADD_SHJBXX(Map param) {
		TB_FWPJ_SHJBXX shjbxx = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_SHJBXX.class);
		shjbxx.setID(shjbxx.uuid());
		shjbxx.setCREATE_TIME(getOpTime(param));
		shjbxx.setOP_ITEM(shjbxx.getCREATE_TIME());
		shjbxx.setOPID(getOpid(param));
		shjbxx.setSTATUS(TB_FWPJ_SHJBXX.STATUS_NEW);
		getJpaDao().save(shjbxx);
		return ObjectUtils.javaBeanToMap(shjbxx);
	}

	@Override
	public Map UPD_SHJBXX(Map param) {

		TB_FWPJ_SHJBXX dto = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_SHJBXX.class);
		TB_FWPJ_SHJBXX po = getJpaDao().find(TB_FWPJ_SHJBXX.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_SHJBXX(Map param) {
		TB_FWPJ_SHJBXX dto = ObjectUtils.mapToJavaBean(param,
				TB_FWPJ_SHJBXX.class);
		TB_FWPJ_SHJBXX po = getJpaDao().find(TB_FWPJ_SHJBXX.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_FWPJ_LXDY.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map SAVE_PJSQ_KZSX(Map param) {
		List LIST_TB_FWPJ_SHJBXX_PJSQ_KZSX = ObjectUtils.getMapValue(param,
				"LIST_TB_FWPJ_SHJBXX_PJSQ_KZSX", List.class);
		if (LIST_TB_FWPJ_SHJBXX_PJSQ_KZSX != null) {
			for (Object item : LIST_TB_FWPJ_SHJBXX_PJSQ_KZSX) {
				TB_FWPJ_SHJBXX_PJSQ_KZSX kzsx = ObjectUtils.mapToJavaBean(
						(Map) item, TB_FWPJ_SHJBXX_PJSQ_KZSX.class);
				kzsx.setOP_ITEM(getOpTime(param));
				kzsx.setOPID(getOpid(param));
				String shid = (String) param.get("ID");
				if ((kzsx.getSHID() == null || kzsx.getSHID().trim() == "")
						&& shid != null && shid.trim() != "")
					kzsx.setSHID(shid);
				if (kzsx.getID() == null) {
					if (kzsx.getSXZ() != null && !kzsx.getSXZ().isEmpty()) {
						kzsx.setID(kzsx.uuid());
						kzsx.setCREATE_TIME(getOpTime(param));
						kzsx.setSTATUS(0);
						getJpaDao().save(kzsx);
						((Map) item).put("ID", kzsx.getID());
					}
				} else {
					TB_FWPJ_SHJBXX_PJSQ_KZSX po = getJpaDao().find(
							TB_FWPJ_SHJBXX_PJSQ_KZSX.class, kzsx.getID());
					ObjectUtils.copyJavaBeanToBean(kzsx, po, "CREATE_TIME",
							"STATUS");
					getJpaDao().update(po);
				}
			}
		}
		SAVE_KZSX((Map) getServiceInterface().doService("SVC_FWPJ_RULE",
				"rule", param));
		return param;
	}

	@Override
	public Map SAVE_KZSX(Map param) {
		System.out.print("\n\nSAVE_KZSX\n\n");
		List LIST_TB_FWPJ_SHJBXX_KZSX = ObjectUtils.getMapValue(param,
				"LIST_TB_FWPJ_SHJBXX_KZSX", List.class);
		System.out.print("\n\n" + LIST_TB_FWPJ_SHJBXX_KZSX);
		if (LIST_TB_FWPJ_SHJBXX_KZSX != null) {
			Iterator iterator = LIST_TB_FWPJ_SHJBXX_KZSX.iterator();
			while (iterator.hasNext()) {
				TB_FWPJ_SHJBXX_PJSQ_KZSX node = (TB_FWPJ_SHJBXX_PJSQ_KZSX) iterator
						.next();
				HashMap c_param = new HashMap();
				TB_FWPJ_SHJBXX_KZSX kzsx = ObjectUtils.mapToJavaBean(c_param,
						TB_FWPJ_SHJBXX_KZSX.class);
				ObjectUtils.copyJavaBeanToBean(node, kzsx, "UILX",
						"CREATE_TIME");
				TB_FWPJ_SHJBXX_KZSX po = getJpaDao().find(
						TB_FWPJ_SHJBXX_KZSX.class, kzsx.getID());
				if (po == null) {
					if (kzsx.getSXZ() != null && !kzsx.getSXZ().isEmpty())
						getJpaDao().save(kzsx);
				} else {
					ObjectUtils.copyJavaBeanToBean(kzsx, po, "CREATE_TIME",
							"STATUS");
					getJpaDao().update(po);
				}
			}
		}

		return param;
	}

	@Override
	public Map SAVE_KZSX_PDJG(Map param) {
		List LIST_TB_FWPJ_SHJBXX_PDJG_KZSX = ObjectUtils.getMapValue(param,
				"LIST_TB_FWPJ_SHJBXX_PDJG_KZSX", List.class);
		if (LIST_TB_FWPJ_SHJBXX_PDJG_KZSX != null) {
			for (Object item : LIST_TB_FWPJ_SHJBXX_PDJG_KZSX) {
				TB_FWPJ_SHJBXX_PDJG_KZSX kzsx = ObjectUtils.mapToJavaBean(
						(Map) item, TB_FWPJ_SHJBXX_PDJG_KZSX.class);
				kzsx.setOP_ITEM(getOpTime(param));
				kzsx.setOPID(getOpid(param));
				String shid = (String) param.get("ID");
				if ((kzsx.getSHID() == null || kzsx.getSHID().trim() == "")
						&& shid != null && shid.trim() != "")
					kzsx.setSHID(shid);
				TB_FWPJ_SHJBXX_PDJG_KZSX po = getJpaDao().find(
						TB_FWPJ_SHJBXX_PDJG_KZSX.class, kzsx.getID());
				if (po == null) {
					if (kzsx.getSXZ() != null && !kzsx.getSXZ().isEmpty()) {
						kzsx.setCREATE_TIME(getOpTime(param));
						kzsx.setSTATUS(0);
						getJpaDao().save(kzsx);
					}
				} else {
					ObjectUtils.copyJavaBeanToBean(kzsx, po, "CREATE_TIME",
							"STATUS");
					getJpaDao().update(po);
				}
			}
		}
		return param;
	}

	@Override
	public Map ADD_BZGLYLZC(Map param) {
		TB_FWPJ_YLZC bo = ObjectUtils.mapToJavaBean(param, TB_FWPJ_YLZC.class);
		bo.setID(bo.uuid());
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(bo.getCREATE_TIME());
		bo.setOPID(getOpid(param));
		bo.setSTATUS(TB_FWPJ_YLZC.STATUS_ACTIVITY);
		getJpaDao().save(bo);
		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_BZGLYLZC(Map param) {

		TB_FWPJ_YLZC dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_YLZC.class);
		TB_FWPJ_YLZC po = getJpaDao().find(TB_FWPJ_YLZC.class, dto.getID());

		ObjectUtils.copyJavaBeanToBean(dto, po, "CREATE_TIME");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_BZGLYLZC(Map param) {
		TB_FWPJ_YLZC dto = ObjectUtils.mapToJavaBean(param, TB_FWPJ_YLZC.class);
		TB_FWPJ_YLZC po = getJpaDao().find(TB_FWPJ_YLZC.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_FWPJ_LXDY.STATUS_DEAD);
		getJpaDao().update(po);
	}

}
